package com.ty.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.ty.common.utils.R;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import com.ty.entity.Customer;
import com.ty.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (Customer)表控制层
 *
 * @author makejava
 * @since 2020-12-01 22:17:49
 */
@RestController
@RequestMapping("customer")
@Slf4j
public class CustomerController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private CustomerService customerService;



    @PostMapping("upload")
    public R upload(MultipartFile file, HttpServletRequest request) throws IOException {
        //获取上传路径
        String realPath = request.getServletContext().getRealPath("/upload");
        File file1 = new File(realPath);
        //没有此文件则创建
        if (!file1.exists()) file1.mkdirs();

        //获取上传文件的名称
        String filename = file.getOriginalFilename();
        //上传文件路径
        String uploadPath = realPath + File.separator + filename;
        //上传文件
        file.transferTo(Paths.get(uploadPath));
        /*file.transferTo(new File(uploadPath));*/

        ArrayList<Customer> customers = new ArrayList<>();

        Workbook wb = new HSSFWorkbook(new FileInputStream(uploadPath));
        HSSFSheet sheet1 = (HSSFSheet) wb.getSheet("sheet1");
        sheet1.forEach(row -> {
            //过滤第一行标题
            if (row.getRowNum() == 0){
                return;
            }
            String name = row.getCell(0).getStringCellValue();
            row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
            String phone = row.getCell(1).getStringCellValue();
            String date = row.getCell(2).getStringCellValue();
            String idcard = row.getCell(3).getStringCellValue();
            String car = row.getCell(4).getStringCellValue();
            String sex = row.getCell(5).getStringCellValue();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            Date createtime=null;
            try {
                createtime = format.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Customer customer = new Customer();
            customer.setName(name);
            customer.setPhone(phone);
            customer.setCreatetime(createtime);
            customer.setIdcard(idcard);
            customer.setCar(car);
            customer.setSex(sex);
            customers.add(customer);
        });
        //存储到数据库
        boolean b = this.customerService.saveBatch(customers);
        return R.ok().put("isSuccess", b);
    }

    /**
     * 分页查询所有数据
     *
     * @param page     分页对象
     * @param customer 查询实体
     * @return 所有数据
     */
    @PostMapping("page")
    public R selectAll(
            Customer customer,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        PageInfo<Customer> pageInfo = this.customerService.getPage(customer, page, limit);
        return R.ok().put("page", pageInfo);
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return R.ok().put("customers", this.customerService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param customer 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody Customer customer) {
        return R.ok().put("customers", this.customerService.save(customer));
    }

    /**
     * 修改数据
     *
     * @param customer 实体对象
     * @return 修改结果
     */
    @PostMapping("update")
    public R update(Customer customer) {
        log.info("修改传递的数据为{}", customer);
        return R.ok().put("customers", this.customerService.updateById(customer));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return R.ok().put("customers", this.customerService.removeByIds(idList));
    }
}